package com.ht.medicaltreatment.activity

import android.view.View
import androidx.activity.viewModels
import com.gym.baselib.base.BaseActivity
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.viewmodel.CentrifugeParamsViewModel
import com.xgst.lib.utils.showToast
import kotlinx.android.synthetic.main.activity_param_set_layout.btnSave
import kotlinx.android.synthetic.main.activity_param_set_layout.etCentrifegeTime
import kotlinx.android.synthetic.main.activity_param_set_layout.etDownTime
import kotlinx.android.synthetic.main.activity_param_set_layout.etRotationSpeed
import kotlinx.android.synthetic.main.activity_param_set_layout.etTem
import kotlinx.android.synthetic.main.activity_param_set_layout.etUpTime
import kotlinx.android.synthetic.main.activity_param_set_layout.imgUnit
import kotlinx.android.synthetic.main.activity_param_set_layout.tvBrand
import kotlinx.android.synthetic.main.activity_param_set_layout.tvModel
import kotlinx.android.synthetic.main.activity_param_set_layout.tvName
import kotlinx.android.synthetic.main.activity_param_set_layout.tvUnit

class CentrifugeParamsSetActivity:BaseActivity(), View.OnClickListener {
    override fun getLayoutId() = R.layout.activity_param_set_layout

    private val model by viewModels<CentrifugeParamsViewModel>()
    private var id = 0
    override fun init() {
        setTitle("参数设置")
        val name = intent.getStringExtra("name")
        val brand = intent.getStringExtra("brand")
        val model = intent.getStringExtra("model")
        val accTime = intent.getStringExtra("accTime")
        val downTime = intent.getStringExtra("downTime")
        val centrifugeTime = intent.getStringExtra("centrifugeTime")
        val speed = intent.getStringExtra("speed")
        val temperature = intent.getStringExtra("temperature")

        id = intent.getIntExtra("id",0)
        tvName.text = name
        tvBrand.text = brand
        tvModel.text = model
        etUpTime.setText(accTime?.substring(0,accTime.length-3))
        etDownTime.setText(downTime?.substring(0,downTime.length-3))

        etCentrifegeTime.setText(centrifugeTime)
        etRotationSpeed.setText(speed?.substring(0,speed.length-5))
        etTem.setText(temperature?.substring(0,temperature.length-1))

        tvUnit.setOnClickListener(this)
        imgUnit.setOnClickListener(this)
        btnSave.setOnClickListener(this)

        initData()
    }

    private fun initData() {
        model.requestInfo("$id")
    }

    override fun onClick(v: View?) {
        when(v){
            tvUnit,
            imgUnit -> {

            }
            btnSave -> {
                val etUpTime = etUpTime.text.toString()
                val etDownTime = etDownTime.text.toString()
                val etCentrifegeTime = etCentrifegeTime.text.toString()
                val etRotationSpeed = etRotationSpeed.text.toString()
                val etTem = etTem.text.toString()
                if(etUpTime.isEmpty()||
                    etDownTime.isEmpty()||
                    etCentrifegeTime.isEmpty()||
                    etRotationSpeed.isEmpty()||
                    etTem.isEmpty()
                ){
                    showToast("请填写完整数据")
                   return
                }
                showProgress()
                val map = mutableMapOf<String,String>()
                map.put("time",etCentrifegeTime+"min")
                map.put("accTime",etUpTime+"min")
                map.put("downTime",etDownTime+"min")
                map.put("rotationRate","${etRotationSpeed}r/min")
                map.put("temperate",etTem+"℃")
                map.put("id","$id")
                model.changParams(map,{
                    dismissProgress()
                    setResult(RESULT_OK)
                    finish()
                },{
                    dismissProgress()
                    showToast(it)
                })
            }
        }
    }
}